package cn.com.open.errorquestion.service.impl;

import cn.com.open.errorquestion.model.AnswerQuestionLog;
import cn.com.open.errorquestion.service.AnswerQuestionLogService;
import cn.com.open.errorquestion.service.KafkaReceiverService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Optional;


@Service
public class KafkaReceiverServiceImpl implements KafkaReceiverService {

    @Autowired
    private AnswerQuestionLogService answerQuestionLogService;


    @KafkaListener(topics = {"error-question"})
    public void listen(ConsumerRecord<?, ?> record) {
        Optional<?> value = Optional.ofNullable(record.value());
        if(value.isPresent()){
            ArrayList<AnswerQuestionLog> answerDatas = JSON.parseObject((String) value.get(), new TypeReference<ArrayList<AnswerQuestionLog>>() {});
            answerQuestionLogService.saveQuestion(answerDatas);
        }
    }
}
